<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libfuse: example/poll.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">libfuse
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_cfafba98a580ce4b62f8a6fa96d7cbb0.html">example</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">poll.c File Reference</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include &lt;config.h&gt;</code><br />
<code>#include &lt;<a class="el" href="fuse_8h_source.html">fuse.h</a>&gt;</code><br />
<code>#include &lt;unistd.h&gt;</code><br />
<code>#include &lt;ctype.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;errno.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
<code>#include &lt;pthread.h&gt;</code><br />
<code>#include &lt;poll.h&gt;</code><br />
</div>
<p><a href="poll_8c_source.html">Go to the source code of this file.</a></p>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This example illustrates how to write a FUSE file system that supports polling for changes that don't come through the kernel. It can be tested with the <a class="el" href="poll__client_8c.html">poll_client.c</a> program.</p>
<p>Compile with: </p><pre class="fragment">gcc -Wall poll.c `pkg-config fuse3 --cflags --libs` -o poll
</pre><h2>Source code</h2>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  FUSE fsel: FUSE select example</span></div><div class="line"><span class="comment">  Copyright (C) 2008       SUSE Linux Products GmbH</span></div><div class="line"><span class="comment">  Copyright (C) 2008       Tejun Heo &lt;teheo@suse.de&gt;</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  This program can be distributed under the terms of the GNU GPL.</span></div><div class="line"><span class="comment">  See the file COPYING.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define FUSE_USE_VERSION 31</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;config.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="fuse_8h.html">fuse.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;ctype.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;errno.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;pthread.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;poll.h&gt;</span></div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * fsel_open_mask is used to limit the number of opens to 1 per file.</span></div><div class="line"><span class="comment"> * This is to use file index (0-F) as fh as poll support requires</span></div><div class="line"><span class="comment"> * unique fh per open file.  Lifting this would require proper open</span></div><div class="line"><span class="comment"> * file management.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">unsigned</span> fsel_open_mask;</div><div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> fsel_hex_map[] = <span class="stringliteral">&quot;0123456789ABCDEF&quot;</span>;</div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span>fuse *fsel_fuse;  <span class="comment">/* needed for poll notification */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define FSEL_CNT_MAX    10      </span><span class="comment">/* each file can store upto 10 chars */</span><span class="preprocessor"></span></div><div class="line"><span class="preprocessor">#define FSEL_FILES      16</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> pthread_mutex_t fsel_mutex;      <span class="comment">/* protects notify_mask and cnt array */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">unsigned</span> fsel_poll_notify_mask;  <span class="comment">/* poll notification scheduled? */</span></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span>fuse_pollhandle *fsel_poll_handle[FSEL_FILES]; <span class="comment">/* poll notify handles */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">unsigned</span> fsel_cnt[FSEL_FILES];   <span class="comment">/* nbytes stored in each file */</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_path_index(<span class="keyword">const</span> <span class="keywordtype">char</span> *path)</div><div class="line">{</div><div class="line">        <span class="keywordtype">char</span> ch = path[1];</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (strlen(path) != 2 || path[0] != <span class="charliteral">&#39;/&#39;</span> || !isxdigit(ch) || islower(ch))</div><div class="line">                <span class="keywordflow">return</span> -1;</div><div class="line">        <span class="keywordflow">return</span> ch &lt;= <span class="charliteral">&#39;9&#39;</span> ? ch - <span class="charliteral">&#39;0&#39;</span> : ch - <span class="charliteral">&#39;A&#39;</span> + 10;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_getattr(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> stat *stbuf,</div><div class="line">                        <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line">        <span class="keywordtype">int</span> idx;</div><div class="line"></div><div class="line">        memset(stbuf, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> stat));</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (strcmp(path, <span class="stringliteral">&quot;/&quot;</span>) == 0) {</div><div class="line">                stbuf-&gt;st_mode = S_IFDIR | 0555;</div><div class="line">                stbuf-&gt;st_nlink = 2;</div><div class="line">                <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line"></div><div class="line">        idx = fsel_path_index(path);</div><div class="line">        <span class="keywordflow">if</span> (idx &lt; 0)</div><div class="line">                <span class="keywordflow">return</span> -ENOENT;</div><div class="line"></div><div class="line">        stbuf-&gt;st_mode = S_IFREG | 0444;</div><div class="line">        stbuf-&gt;st_nlink = 1;</div><div class="line">        stbuf-&gt;st_size = fsel_cnt[idx];</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_readdir(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">void</span> *buf, <a class="code" href="fuse_8h.html#a7dd132de66a5cc2add2a4eff5d435660">fuse_fill_dir_t</a> filler,</div><div class="line">                        off_t offset, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi,</div><div class="line">                        <span class="keyword">enum</span> <a class="code" href="fuse_8h.html#af2bcf2a473b41b3cc8da8c079656a074">fuse_readdir_flags</a> flags)</div><div class="line">{</div><div class="line">        <span class="keywordtype">char</span> name[2] = { };</div><div class="line">        <span class="keywordtype">int</span> i;</div><div class="line"></div><div class="line">        (void) offset;</div><div class="line">        (void) fi;</div><div class="line">        (void) flags;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (strcmp(path, <span class="stringliteral">&quot;/&quot;</span>) != 0)</div><div class="line">                <span class="keywordflow">return</span> -ENOENT;</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (i = 0; i &lt; FSEL_FILES; i++) {</div><div class="line">                name[0] = fsel_hex_map[i];</div><div class="line">                filler(buf, name, NULL, 0, 0);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_open(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        <span class="keywordtype">int</span> idx = fsel_path_index(path);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (idx &lt; 0)</div><div class="line">                <span class="keywordflow">return</span> -ENOENT;</div><div class="line">        <span class="keywordflow">if</span> ((fi-&gt;<a class="code" href="structfuse__file__info.html#ae7d31802727be19670193a411647bca5">flags</a> &amp; 3) != O_RDONLY)</div><div class="line">                <span class="keywordflow">return</span> -EACCES;</div><div class="line">        <span class="keywordflow">if</span> (fsel_open_mask &amp; (1 &lt;&lt; idx))</div><div class="line">                <span class="keywordflow">return</span> -EBUSY;</div><div class="line">        fsel_open_mask |= (1 &lt;&lt; idx);</div><div class="line"></div><div class="line">        <span class="comment">/*</span></div><div class="line"><span class="comment">         * fsel files are nonseekable somewhat pipe-like files which</span></div><div class="line"><span class="comment">         * gets filled up periodically by producer thread and consumed</span></div><div class="line"><span class="comment">         * on read.  Tell FUSE as such.</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        fi-&gt;<a class="code" href="structfuse__file__info.html#a45314d0b92a8d4c9de33d996aa59ada8">fh</a> = idx;</div><div class="line">        fi-&gt;<a class="code" href="structfuse__file__info.html#a03b59a10e62963d9affa34ad78bd144a">direct_io</a> = 1;</div><div class="line">        fi-&gt;<a class="code" href="structfuse__file__info.html#a272022c57a6a79dd8f98ef597786e154">nonseekable</a> = 1;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_release(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        <span class="keywordtype">int</span> idx = fi-&gt;<a class="code" href="structfuse__file__info.html#a45314d0b92a8d4c9de33d996aa59ada8">fh</a>;</div><div class="line"></div><div class="line">        (void) path;</div><div class="line"></div><div class="line">        fsel_open_mask &amp;= ~(1 &lt;&lt; idx);</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_read(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> size, off_t offset,</div><div class="line">                     <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        <span class="keywordtype">int</span> idx = fi-&gt;<a class="code" href="structfuse__file__info.html#a45314d0b92a8d4c9de33d996aa59ada8">fh</a>;</div><div class="line"></div><div class="line">        (void) path;</div><div class="line">        (void) offset;</div><div class="line"></div><div class="line">        pthread_mutex_lock(&amp;fsel_mutex);</div><div class="line">        <span class="keywordflow">if</span> (fsel_cnt[idx] &lt; size)</div><div class="line">                size = fsel_cnt[idx];</div><div class="line">        printf(<span class="stringliteral">&quot;READ   %X transferred=%zu cnt=%u\n&quot;</span>, idx, size, fsel_cnt[idx]);</div><div class="line">        fsel_cnt[idx] -= size;</div><div class="line">        pthread_mutex_unlock(&amp;fsel_mutex);</div><div class="line"></div><div class="line">        memset(buf, fsel_hex_map[idx], size);</div><div class="line">        <span class="keywordflow">return</span> size;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fsel_poll(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi,</div><div class="line">                     <span class="keyword">struct</span> fuse_pollhandle *ph, <span class="keywordtype">unsigned</span> *reventsp)</div><div class="line">{</div><div class="line">        <span class="keyword">static</span> <span class="keywordtype">unsigned</span> polled_zero;</div><div class="line">        <span class="keywordtype">int</span> idx = fi-&gt;<a class="code" href="structfuse__file__info.html#a45314d0b92a8d4c9de33d996aa59ada8">fh</a>;</div><div class="line"></div><div class="line">        (void) path;</div><div class="line"></div><div class="line">        <span class="comment">/*</span></div><div class="line"><span class="comment">         * Poll notification requires pointer to struct fuse which</span></div><div class="line"><span class="comment">         * can&#39;t be obtained when using fuse_main().  As notification</span></div><div class="line"><span class="comment">         * happens only after poll is called, fill it here from</span></div><div class="line"><span class="comment">         * fuse_context.</span></div><div class="line"><span class="comment">         */</span></div><div class="line">        <span class="keywordflow">if</span> (!fsel_fuse) {</div><div class="line">                <span class="keyword">struct </span><a class="code" href="structfuse__context.html">fuse_context</a> *cxt = <a class="code" href="fuse_8h.html#a9a0f2a6603cb1e21f5e4c1aec84a3bcc">fuse_get_context</a>();</div><div class="line">                <span class="keywordflow">if</span> (cxt)</div><div class="line">                        fsel_fuse = cxt-&gt;<a class="code" href="structfuse__context.html#a8872cbd7e8d0269fb5cbd1ed40e60a19">fuse</a>;</div><div class="line">        }</div><div class="line"></div><div class="line">        pthread_mutex_lock(&amp;fsel_mutex);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (ph != NULL) {</div><div class="line">                <span class="keyword">struct </span>fuse_pollhandle *oldph = fsel_poll_handle[idx];</div><div class="line"></div><div class="line">                <span class="keywordflow">if</span> (oldph)</div><div class="line">                        <a class="code" href="fuse__common_8h.html#adf5027f8a38b2efc03858efd7fdc756a">fuse_pollhandle_destroy</a>(oldph);</div><div class="line"></div><div class="line">                fsel_poll_notify_mask |= (1 &lt;&lt; idx);</div><div class="line">                fsel_poll_handle[idx] = ph;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (fsel_cnt[idx]) {</div><div class="line">                *reventsp |= POLLIN;</div><div class="line">                printf(<span class="stringliteral">&quot;POLL   %X cnt=%u polled_zero=%u\n&quot;</span>,</div><div class="line">                       idx, fsel_cnt[idx], polled_zero);</div><div class="line">                polled_zero = 0;</div><div class="line">        } <span class="keywordflow">else</span></div><div class="line">                polled_zero++;</div><div class="line"></div><div class="line">        pthread_mutex_unlock(&amp;fsel_mutex);</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structfuse__operations.html">fuse_operations</a> fsel_oper = {</div><div class="line">        .<a class="code" href="structfuse__operations.html#ac39a0b7125a0e5001eb5ff42e05faa5d">getattr</a>        = fsel_getattr,</div><div class="line">        .readdir        = fsel_readdir,</div><div class="line">        .open           = fsel_open,</div><div class="line">        .release        = fsel_release,</div><div class="line">        .read           = fsel_read,</div><div class="line">        .poll           = fsel_poll,</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> *fsel_producer(<span class="keywordtype">void</span> *data)</div><div class="line">{</div><div class="line">        <span class="keyword">const</span> <span class="keyword">struct </span>timespec interval = { 0, 250000000 };</div><div class="line">        <span class="keywordtype">unsigned</span> idx = 0, nr = 1;</div><div class="line"></div><div class="line">        (void) data;</div><div class="line"></div><div class="line">        <span class="keywordflow">while</span> (1) {</div><div class="line">                <span class="keywordtype">int</span> i, t;</div><div class="line"></div><div class="line">                pthread_mutex_lock(&amp;fsel_mutex);</div><div class="line"></div><div class="line">                <span class="comment">/*</span></div><div class="line"><span class="comment">                 * This is the main producer loop which is executed</span></div><div class="line"><span class="comment">                 * ever 500ms.  On each iteration, it fills one byte</span></div><div class="line"><span class="comment">                 * to 1, 2 or 4 files and sends poll notification if</span></div><div class="line"><span class="comment">                 * requested.</span></div><div class="line"><span class="comment">                 */</span></div><div class="line">                <span class="keywordflow">for</span> (i = 0, t = idx; i &lt; nr;</div><div class="line">                     i++, t = (t + FSEL_FILES / nr) % FSEL_FILES) {</div><div class="line">                        <span class="keywordflow">if</span> (fsel_cnt[t] == FSEL_CNT_MAX)</div><div class="line">                                <span class="keywordflow">continue</span>;</div><div class="line"></div><div class="line">                        fsel_cnt[t]++;</div><div class="line">                        <span class="keywordflow">if</span> (fsel_fuse &amp;&amp; (fsel_poll_notify_mask &amp; (1 &lt;&lt; t))) {</div><div class="line">                                <span class="keyword">struct </span>fuse_pollhandle *ph;</div><div class="line"></div><div class="line">                                printf(<span class="stringliteral">&quot;NOTIFY %X\n&quot;</span>, t);</div><div class="line">                                ph = fsel_poll_handle[t];</div><div class="line">                                fuse_notify_poll(ph);</div><div class="line">                                <a class="code" href="fuse__common_8h.html#adf5027f8a38b2efc03858efd7fdc756a">fuse_pollhandle_destroy</a>(ph);</div><div class="line">                                fsel_poll_notify_mask &amp;= ~(1 &lt;&lt; t);</div><div class="line">                                fsel_poll_handle[t] = NULL;</div><div class="line">                        }</div><div class="line">                }</div><div class="line"></div><div class="line">                idx = (idx + 1) % FSEL_FILES;</div><div class="line">                <span class="keywordflow">if</span> (idx == 0)</div><div class="line">                        nr = (nr * 2) % 7;      <span class="comment">/* cycle through 1, 2 and 4 */</span></div><div class="line"></div><div class="line">                pthread_mutex_unlock(&amp;fsel_mutex);</div><div class="line"></div><div class="line">                nanosleep(&amp;interval, NULL);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> NULL;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">        pthread_t producer;</div><div class="line">        pthread_attr_t attr;</div><div class="line">        <span class="keywordtype">int</span> ret;</div><div class="line"></div><div class="line">        errno = pthread_mutex_init(&amp;fsel_mutex, NULL);</div><div class="line">        <span class="keywordflow">if</span> (errno) {</div><div class="line">                perror(<span class="stringliteral">&quot;pthread_mutex_init&quot;</span>);</div><div class="line">                <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line"></div><div class="line">        errno = pthread_attr_init(&amp;attr);</div><div class="line">        <span class="keywordflow">if</span> (errno) {</div><div class="line">                perror(<span class="stringliteral">&quot;pthread_attr_init&quot;</span>);</div><div class="line">                <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line"></div><div class="line">        errno = pthread_create(&amp;producer, &amp;attr, fsel_producer, NULL);</div><div class="line">        <span class="keywordflow">if</span> (errno) {</div><div class="line">                perror(<span class="stringliteral">&quot;pthread_create&quot;</span>);</div><div class="line">                <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line"></div><div class="line">        ret = <a class="code" href="fuse_8h.html#ac99b844cee7aaa8fb4e35df5b5488d82">fuse_main</a>(argc, argv, &amp;fsel_oper, NULL);</div><div class="line"></div><div class="line">        pthread_cancel(producer);</div><div class="line">        pthread_join(producer, NULL);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> ret;</div><div class="line">}</div></div><!-- fragment --> 
<p class="definition">Definition in file <a class="el" href="poll_8c_source.html">poll.c</a>.</p>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
